"""
Check a page to make sure we can use it.
"""

import urllib2, httplib

__all__ = ('check_page',)

def check_page(url):
    request = urllib2.Request(url, headers={'User-Agent': 'Mozilla/4.0'})
    opener = urllib2.build_opener(MyRedirectHandler)
    try:
        f = opener.open(request)
    except Exception, e:
        print "Error: %s" % e
        return None

    contents = f.read()
    #if 'top.location' in contents:
    #    return None
    if getattr(f, 'new_location', None):
        if f.new_location.startswith('http'):
            return f.new_location
    return url


class MyRedirectHandler(urllib2.HTTPRedirectHandler):

    def http_error_301(self, req, fp, code, msg, headers):
        result = urllib2.HTTPRedirectHandler.http_error_301(
            self, req, fp, code, msg, headers
            )
        result.status = code
        result.new_location = headers.get('Location', None)
        return result

    def http_error_302(self, req, fp, code, msg, headers):
        result = urllib2.HTTPRedirectHandler.http_error_302(
            self, req, fp, code, msg, headers
            )
        result.status = code
        result.new_location = headers.get('Location', None)
        return result
